package com.guangwei.phoneforelder

import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.ActivityInfo
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.telecom.TelecomManager
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private val   phoneNumberMaxSize = 15

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()

        handleButtonClick()
    }

    private fun initView() {
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)

        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }

    override fun onStart() {
        super.onStart()

        // 发起将本应用设为默认电话应用的请求，仅支持 Android M 及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val intent = Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER)
            intent.putExtra(
                TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,
                packageName
            )
            startActivity(intent)
        } else {
            Toast.makeText(this@MainActivity, "Android 6.0 以上才支持修改默认电话应用！", Toast.LENGTH_LONG).show()
        }
    }

    override fun onResume() {
        super.onResume()
        initView()
    }

    override fun onRestart() {
        super.onRestart()
        initView()
    }

    private fun handleButtonClick(){

        btn_clear.setOnClickListener{
            tv_show.text = ""
        }

        btn_dial.setOnClickListener {
            if (tv_show.text.toString().isNotEmpty()) {
                val intent = Intent()
                intent.action = Intent.ACTION_CALL
                intent.data = Uri.parse("tel:" + tv_show.text)
                startActivity(intent)

                // Clear the "phone number"
                tv_show.text = ""
            }
            else {
                Toast.makeText(this, R.string.phone_number_can_not_empty, Toast.LENGTH_LONG).show()
            }
        }

        btn_0.setOnClickListener{
            appendPhoneNumer( "0")
        }

        btn_1.setOnClickListener {
            appendPhoneNumer("1")
        }

        btn_2.setOnClickListener{
            appendPhoneNumer("2")
        }

        btn_3.setOnClickListener{
            appendPhoneNumer( "3")
        }

        btn_4.setOnClickListener{
            appendPhoneNumer("4")
        }

        btn_5.setOnClickListener{
            appendPhoneNumer("5")
        }

        btn_6.setOnClickListener{
            appendPhoneNumer( "6")
        }

        btn_7.setOnClickListener{
            appendPhoneNumer("7")
        }

        btn_8.setOnClickListener{
            appendPhoneNumer("8")
        }

        btn_9.setOnClickListener{
            appendPhoneNumer("9")
        }



    }

    @SuppressLint("SetTextI18n")
    private fun appendPhoneNumer(digit: String) {
        val phoneNumber = tv_show.text
        if (phoneNumber.toString().length < phoneNumberMaxSize) {
            tv_show.text = phoneNumber.toString() + digit
        } else
            Toast.makeText(this, R.string.phone_number_too_long, Toast.LENGTH_LONG).show()
    }


}
